/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.ProcurementSemenCtlr;
import org.gov.vis.data.controller.ProcurementSemenCtlrImpl;
import org.gov.vis.data.dto.ProcurementSemenDto;
import org.gov.vis.data.gui.ProcurementSemenDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ProcurementSemenListener implements ActionListener,BaseListener{




	private ProcurementSemenDialog procurementSemenDailog;
	private ProcurementSemenCtlr procurementSemenCtlr = new ProcurementSemenCtlrImpl();
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private ProcurementSemenDto procurementSemenDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public ProcurementSemenListener(ProcurementSemenDialog procurementSemensDialog){

		this.procurementSemenDailog = procurementSemensDialog;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		try{
			errorMsgPanel = new ErrorsDisplayJPanel(procurementSemenDailog.getjPanel1());
			errorMsgPanel.clearMsgPanel();

			if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
					|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
				//Method called to either Add or Edit the GopalaMithra details
				addOrEditProcurementSemen();
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
				//Method called to view the details of ProcurementSemen Object based on the passed procurementSemenId
				viewProcurementSemen(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
				//Method called to delete the ProcurementSemen Object based on the passed procurementSemenId.
				deleteProcurementSemen(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
				//Method called to clear the screen data
				clear();
			}
		}catch(Exception e){
			e.printStackTrace();
		}

	}//end actionPerformed


	private void viewProcurementSemen(final long _procurementSemenId) {
		// TODO Auto-generated method stub
		try{
			final ProcurementSemenDto _procurementSemenDto = procurementSemenCtlr.getPrSemen(_procurementSemenId);
			setDtoDetailsToGui(_procurementSemenDto);
			procurementSemenDailog.validate();
			procurementSemenDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Method called to delete the ProcurementSemen Object based on the passed procurementSemenId
	 * @param _procurementSemenId
	 */
	private void deleteProcurementSemen(final long  _procurementSemenId){
		try{
			procurementSemenCtlr.deletePrSemen(_procurementSemenId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteProcurementSemen

	/**
	 * Method called to either Add or Edit the Procurement details
	 */
	private void addOrEditProcurementSemen(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(procurementSemenDailog);
		try{
			//Method called to Add or Edit the GopalaMithra
			procurementSemenCtlr.addOrEditPrSemen(procurementSemenDto);

			/*List<String> successMsgLst = new ArrayList<String>();
			successMsgLst.add(rb.getString(GuiConstants.DATA_ADD_PROCUREMENTSEMEN_SUCCESS));
			errorMsgPanel.setSuccessMessages(successMsgLst);
			
			progencyCtlr.addOrEditProgency(progencyDto);*/
			JOptionPane.showMessageDialog(procurementSemenDailog, rb.getString(GuiConstants.DATA_ADD_PROCUREMENTSEMEN_SUCCESS));
			//procurementSemenDailog.dispose();
			

			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = ProcurementSemenCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_code").toUpperCase())){
						procurementSemenDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_bullno").toUpperCase())){
						procurementSemenDailog.getLblBullNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_bullno").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_frmstation").toUpperCase())){
						procurementSemenDailog.getLblFromStation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_frmstation").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_noofStrwas").toUpperCase())){
						procurementSemenDailog.getLblNoOfStraws().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_noofStrwas").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_dtofrcpt").toUpperCase())){
						procurementSemenDailog.getLblDateOfReceipt().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_dtofrcpt").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		
	}//end addOrEditGopalaMithra

	/**
	 * Method called to clear the screen data
	 */
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		procurementSemenDailog.getTxtFldCode().setText("");
		procurementSemenDailog.getTxtFldBreed().setText("");
		procurementSemenDailog.getTxtFldBullNo().setText("");
		procurementSemenDailog.getTxtFldDiscord().setText("");
		procurementSemenDailog.getDtChoDateOfReceipt().setDate(null);
		procurementSemenDailog.getTxtFldFromStation().setText("");
		procurementSemenDailog.getTxtFldNoOfStraws().setText("");

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

	}//end clear

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		final ProcurementSemenDto _procurementSemenDto = (ProcurementSemenDto)object;
		procurementSemenDailog.getTxtFldBreed().setText(_procurementSemenDto.getBreed());
		procurementSemenDailog.getTxtFldBullNo().setText(_procurementSemenDto.getBullNo());
		procurementSemenDailog.getTxtFldDiscord().setText(_procurementSemenDto.getDiscord());
		procurementSemenDailog.getTxtFldDOReceipt().setText(DU.format(_procurementSemenDto.getDoRcpt(),DBConstants.DISPLAY_DATE_FORMAT));
		procurementSemenDailog.getTxtFldFromStation().setText(_procurementSemenDto.getFrmStation());
		procurementSemenDailog.getTxtFldNoOfStraws().setText(_procurementSemenDto.getNoStraws());

	}
	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final ProcurementSemenDialog _procurementSemenDialog = (ProcurementSemenDialog)object;
		procurementSemenDto = new ProcurementSemenDto();
		procurementSemenDto.setCode(_procurementSemenDialog.getTxtFldCode().getText());
		procurementSemenDto.setBreed(_procurementSemenDialog.getTxtFldBreed().getText());
		procurementSemenDto.setBullNo(_procurementSemenDialog.getTxtFldBullNo().getText());
		procurementSemenDto.setDiscord(_procurementSemenDialog.getTxtFldDiscord().getText());
		if(_procurementSemenDialog.getDtChoDateOfReceipt().getDate() != null){
			procurementSemenDto.setDoRpt(_procurementSemenDialog.getDtChoDateOfReceipt().getDate());
		}
		procurementSemenDto.setFrmStation(_procurementSemenDialog.getTxtFldFromStation().getText());
		procurementSemenDto.setNoStraws(_procurementSemenDialog.getTxtFldNoOfStraws().getText());

	}

	public void setProcurementSemensDailog(final ProcurementSemenDialog _distrbutionSemenDailog) {
		this.procurementSemenDailog = _distrbutionSemenDailog;
	}

	public ProcurementSemenDialog getGopalaMithrasDailog() {
		return procurementSemenDailog;
	}

	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		procurementSemenDailog.getLblCode().setForeground(_lblColor);
		procurementSemenDailog.getLblBreed().setForeground(_lblColor);
		procurementSemenDailog.getLblBullNo().setForeground(_lblColor);
		procurementSemenDailog.getLblDateOfReceipt().setForeground(_lblColor);
		procurementSemenDailog.getLblDiscord().setForeground(_lblColor);
		procurementSemenDailog.getLblFromStation().setForeground(_lblColor);
		procurementSemenDailog.getLblNoOfStraws().setForeground(_lblColor);


	}


}//end class
